package com.example.service.impl;

import com.example.entity.dto.EmployeeDto;
import com.example.entity.po.Employee;
import com.example.entity.vo.EmployeeVo;
import com.example.mapper.EmployeeMapper;
import com.example.service.EmployeeService;
import com.example.utils.BeanUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author hongyi
 * @date 2024/8/24 16:27
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public int insert(EmployeeDto employeeDto) {
        Employee employee = new Employee();
        employee.setHireDate(LocalDateTime.now());
        BeanUtils.copyProperties(employeeDto,employee);
        return employeeMapper.insert(employee);
    }

    @Override
    public int deleteById(Integer id) {
        return employeeMapper.deleteById(id);
    }

    @Override
    public int updateById(EmployeeDto employeeDto) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDto,employee);
        return employeeMapper.updateById(employee);
    }

    @Override
    public Employee selectById(Integer id) {
        return employeeMapper.selectById(id);
    }

    @Override
    public List<EmployeeVo> selectAll(Employee employee) {
        return employeeMapper.selectAll(employee);
    }

    /**
     *  进行分页
     * @param employee
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<EmployeeVo> selectPage(Employee employee, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<EmployeeVo> list = employeeMapper.selectAll(employee);
        return PageInfo.of(list);
    }

    @Override
    public Employee selectByUsername(String name) {
        return employeeMapper.selectByUsername(name);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        for (int id: ids) {
            employeeMapper.deleteById(id);
        }
    }
}
